package com.distribution.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.distribution.common.exception.ServiceException;
import com.distribution.system.domain.entity.CmccDaDistanceFlow;
import com.distribution.system.domain.entity.CmccDaProduct;
import com.distribution.system.mapper.CmccDaDistanceFlowMapper;
import com.distribution.system.service.CmccDaProductService;
import com.distribution.system.service.CmccDaDistanceFlowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

/**
 * @author: GodLu
 * @create: 2024-04-09 09:21
 * @description: TODO
 */
@Service("cmccDaDistanceFlowService")
public class CmccDaDistanceFlowServiceImpl extends ServiceImpl<CmccDaDistanceFlowMapper, CmccDaDistanceFlow> implements CmccDaDistanceFlowService {

    @Autowired
    private CmccDaProductService cmccDaProductService;

    @Override
    public List<CmccDaDistanceFlow> getDistanceInfoListByProductId(Integer productId) {
        //根据当前卡id查询分流信息
        List<CmccDaDistanceFlow> distanceFlowArrayList = list(new LambdaQueryWrapper<CmccDaDistanceFlow>()
                .eq(CmccDaDistanceFlow::getProductId, productId)
                .orderByDesc(CmccDaDistanceFlow::getCreateTime));
        if (distanceFlowArrayList == null || distanceFlowArrayList.isEmpty()) throw new ServiceException("当前产品没有异地分流信息");
        return distanceFlowArrayList;
    }

    @Override
    public void addDistanceFlowInfo(Integer currentId, Integer distanceFlowId) {
        //System.out.println(id);
        //System.out.println(areaFlowId);
        //首先当前号卡和分流号卡必须存在
        CmccDaProduct currentCard = cmccDaProductService.getOne(new LambdaQueryWrapper<CmccDaProduct>().eq(CmccDaProduct::getId,currentId),false);
        CmccDaProduct areaFlowCard = cmccDaProductService.getOne(new LambdaQueryWrapper<CmccDaProduct>().eq(CmccDaProduct::getId,distanceFlowId),false);
        if (currentCard == null || areaFlowCard == null)
            throw new ServiceException("当前卡或分流卡不存在");

        //其次确保当前分流信息不存在
        CmccDaDistanceFlow areaFlow = getOne(new LambdaQueryWrapper<CmccDaDistanceFlow>().eq(CmccDaDistanceFlow::getProductId, currentId).eq(CmccDaDistanceFlow::getDistanceFlowProductId, distanceFlowId), false);
        if (areaFlow != null) throw new ServiceException("当前分流信息已存在");

        //设置当前卡的分流开关打开
        currentCard.setDistanceFlowFlag("1");
        cmccDaProductService.updateById(currentCard);

        //再者当前号卡与分流号卡的基础佣金要一致
        /*if (!Objects.equals(currentCard.getBasicsCommission(), areaFlowCard.getBasicsCommission())){
            throw new ServiceException("当前卡与分流卡基础佣金不一致");
        }*/

        //其次确保当前号卡没有绑定分流号卡
        List<CmccDaDistanceFlow> list = list(new LambdaQueryWrapper<CmccDaDistanceFlow>().eq(CmccDaDistanceFlow::getProductId, currentId)
                .eq(CmccDaDistanceFlow::getDistanceFlowProductId, distanceFlowId));
        if (!list.isEmpty())
            throw new ServiceException("当前分流已存在");

        //再者不能分流到自己
        if (Objects.equals(currentId, distanceFlowId)) throw new ServiceException("无法分流到号卡本身");

        //添加绑定号卡记录
        CmccDaDistanceFlow cmccDaDistanceFlow = new CmccDaDistanceFlow();
        cmccDaDistanceFlow.setProductId(currentId);
        cmccDaDistanceFlow.setProductName(currentCard.getProductName());
        cmccDaDistanceFlow.setDistanceFlowProductId(distanceFlowId);
        cmccDaDistanceFlow.setDistanceFlowProductName(areaFlowCard.getProductName());
        cmccDaDistanceFlow.setCreateTime(LocalDateTime.now());
        cmccDaDistanceFlow.setUpdateTime(LocalDateTime.now());
        save(cmccDaDistanceFlow);
    }
}
